package cn.bisoft.java.util.common;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

public class HttpUtil {

	public static URI createURI(String schema, String host, int port,
			String path, Map<String, String> params, String encoding) {
		List<NameValuePair> paramList = new ArrayList<NameValuePair>();

		if (null != params && params.size() > 0) {
			for (Entry<String, String> e : params.entrySet()) {
				paramList.add(new BasicNameValuePair(e.getKey(), e.getValue()));
			}
		}

		URI uri = null;
		try {
			uri = URIUtils.createURI(schema, host, port, path, URLEncodedUtils
					.format(paramList, encoding), null);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

		return uri;
	}

	public static URI createURI(String schema, String host, int port,
			String path) {
		URI uri = URI.create(schema + "://" + host + ":" + port + path);
		return uri;
	}

}
